package org.example.chat.Service.Impl;

import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.chat.Mapper.ChatMapper;
import org.example.chat.Pojo.MessageBody;
import org.example.chat.Service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : zhangbo
 * @data : 2024/8/12
 * @description :
 */
@Service
@Slf4j
public class ChatServiceImpl extends ServiceImpl<ChatMapper, MessageBody> implements ChatService {
    @Autowired
    private ChatService chatService;

    public static void main(String[] args) {
        String srt= "11100101 10111100 10100000 11100101 10001101 10011010  00101100 11100100 10111101 10100000 11100101 10100101 10111101 11100101 10111000 10000101 11100101 10010101 10001010 11101111 10111100 10000001";
        String binaryStr1 = "01100101 01101110 01100111 01101100 01101001 01110011 01101000";
        String binaryStr = "110001000010001 111001000110001 100111101100000";
        String string = binaryStr.replaceAll("\\s+", "");
        log.info("处理后的而精致数据:" + string);
        String chinese = binaryToChinese(binaryStr.replace(" ", "").trim());
        Map<Object, Object>[] map = new Map[]{new HashMap<Object, Object>()};
        System.out.println(chinese); // 输出转换后的内容
        System.out.println(new Date().toString());
    }

    public static String binaryToChinese(String binaryStr) {
        int length = binaryStr.length();
        log.info(String.valueOf(length));
        // 将二进制字符串转换为字节数组
        byte[] bytes = new byte[binaryStr.length() / 8];
        for (int i = 0; i < bytes.length * 8; i += 8) {
            bytes[i / 8] = (byte) Integer.parseInt(binaryStr.substring(i, i + 8), 2);
        }
        try {
            // 使用UTF-8编码将字节数组转换为字符串
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param body
     * @return
     */
    @Override
    public String sendAndResult(MessageBody body) {
        //前端页面通过表单输入内容，解析成json格式
        //消息体消息的判断
        body.setWriteTime(new Date().toString());
        body.setUpdateTime(new Date().toString());
        String result=sendMsgToWeChat(body);
        return null;
    }

    private String sendMsgToWeChat(MessageBody body) {
        //调用微信相关接口，发送消息到微信
        String msg=body.getMessage();
        return "";
    }

    //https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
}
